#!/bin/sh
#=======================================================================
# rk4_expl_vs_ncyc_expl.sh
#   Validate RK4 code by comapring it with and Lorenz 4-cycle scheme.
#                                            2012/11/29 Daisuke Hotta
#=======================================================================
set -e
#-----------------------------------------------------------------------
# 0. Set-up environment
#-----------------------------------------------------------------------
compiler=gfortran  # pgi or gfortran
cd ../../..
SPEEDY=`pwd`
#-----------------------------------------------------------------------
# 1. Compile and execute RK4
#-----------------------------------------------------------------------
schemename=rk4_expl
ncycversion=''
# work directory
TMPDIR=${SPEEDY}/model/validation/wk_exec/${schemename}
rm -rf $TMPDIR
mkdir -p $TMPDIR
cd $TMPDIR
#
# 1-step execution
#
### date setting
IYYYY=1981
IMM=01
IDD=01
IHH=00
### build
echo '>>>BEGIN BUILDING SPEEDY MODEL'
cp $SPEEDY/model/source/makefile .
cp $SPEEDY/model/source/*.h .
cp $SPEEDY/model/source/*.f .
cp $SPEEDY/model/source/*.s .

mv par_horres_t30.h atparam.h
mv par_verres.h atparam1.h

cp $SPEEDY/model/ver32.input/cls_*.h .
cp $SPEEDY/model/ver32.input/inpfiles.s .

cp $SPEEDY/model/update/*.h .
cp $SPEEDY/model/update/*.f .
cp $SPEEDY/model/update/makefile .

## scheme specific
cp $SPEEDY/model/update2/${schemename}/*.f .
cp $SPEEDY/model/update2/${schemename}/*.h .
cp $SPEEDY/model/update2/${schemename}/cls_instep.h_30yr ./cls_instep.h
cp $SPEEDY/model/update2/${schemename}/makefile_${compiler} ./makefile

## validation
cp $SPEEDY/model/validation/${schemename}/*.f .

make imp.exe

sed -e 's/=../=..\/..\/../g' <inpfiles.s>inpfiles2.s
sh inpfiles2.s t30

echo '>>>END BUILDING SPEEDY MODEL'
### run
FORT2=0
echo ">>>BEGIN 1-STEP COMPUTATION OF $IYYYY/$IMM/$IDD/$IHH"
echo $FORT2 > fort.2
echo $IYYYY >> fort.2
echo $IMM >> fort.2
echo $IDD >> fort.2
echo $IHH >> fort.2
time ./imp.exe > out.lis1
### clean up
#rm -f *.o
#rm -f imp.exe
echo "NORMAL END"

#-----------------------------------------------------------------------
# 2. Compile and execute Lorenz 4-cycle
#-----------------------------------------------------------------------
schemename=ncyc_expl
ncycversion='_4b'
# work directory
TMPDIR=${SPEEDY}/model/validation/wk_exec/${schemename}${ncycversion}
rm -rf $TMPDIR
mkdir -p $TMPDIR
cd $TMPDIR
#
# 1-step execution
#
### date setting
IYYYY=1981
IMM=01
IDD=01
IHH=00
### build
echo '>>>BEGIN BUILDING SPEEDY MODEL'
cp $SPEEDY/model/source/makefile .
cp $SPEEDY/model/source/*.h .
cp $SPEEDY/model/source/*.f .
cp $SPEEDY/model/source/*.s .

mv par_horres_t30.h atparam.h
mv par_verres.h atparam1.h

cp $SPEEDY/model/ver32.input/cls_*.h .
cp $SPEEDY/model/ver32.input/inpfiles.s .

cp $SPEEDY/model/update/*.h .
cp $SPEEDY/model/update/*.f .
cp $SPEEDY/model/update/makefile .

## scheme specific
cp $SPEEDY/model/update2/${schemename}/*.f .
cp $SPEEDY/model/update2/${schemename}/*.h .
cp $SPEEDY/model/update2/${schemename}/cls_instep.h_30yr ./cls_instep.h
cp $SPEEDY/model/update2/ncyc_parm/ncycparm.h${ncycversion} ./ncycparm.h
cp $SPEEDY/model/update2/${schemename}/makefile_${compiler} ./makefile

## validation
cp $SPEEDY/model/validation/${schemename}/*.f .

make imp.exe

sed -e 's/=../=..\/..\/../g' <inpfiles.s>inpfiles2.s
sh inpfiles2.s t30

echo '>>>END BUILDING SPEEDY MODEL'
### run
FORT2=0
echo ">>>BEGIN 1-STEP COMPUTATION OF $IYYYY/$IMM/$IDD/$IHH"
echo $FORT2 > fort.2
echo $IYYYY >> fort.2
echo $IMM >> fort.2
echo $IDD >> fort.2
echo $IHH >> fort.2
time ./imp.exe > out.lis1
### clean up
#rm -f *.o
#rm -f imp.exe
echo "NORMAL END"

#-----------------------------------------------------------------------
# 3. Validation by taking diff
#-----------------------------------------------------------------------
RSLT=$( diff ${SPEEDY}/model/validation/wk_exec/rk4_expl/1981010100.grd \
    ${SPEEDY}/model/validation/wk_exec/ncyc_expl/1981010100.grd )
if [ -z $RSLT ]; then
  echo "=================================="
  echo " Congratulations !! Results agree "
  echo "=================================="
else
  echo "=================================="
  echo " Sorry, results do not agree      "
  echo "=================================="
fi
